var util = require('../../../utils/util.js');
var api = require('../../../config/api.js');
var user = require('../../../services/user.js');
var app = getApp();

Page({
  data: {
    userInfo: {},
    hasMobile: ''
  },
  //页面初始化 options为页面跳转所带来的参数
  onLoad: function (options) {},
  //页面显示
  onShow: function () {
    let userInfo = wx.getStorageSync('userInfo');
    let token = wx.getStorageSync('token');
    // 页面显示
    if (userInfo) {
      app.globalData.userInfo = userInfo;
      app.globalData.token = token;
    }
    this.setData({
      userInfo: app.globalData.userInfo,
    });
  },
  //得到用户信息进行登录
  bindGetUserInfo(e) {
    let userInfo = wx.getStorageSync('userInfo');
    let token = wx.getStorageSync('token');
    if (userInfo && token) {
      return;
    }
    if (e.detail.userInfo) {
      //用户按了允许授权按钮
      user.loginByWeixin(e.detail).then(res => {
        this.setData({
          userInfo: res.data.userInfo
        });
        app.globalData.userInfo = res.data.userInfo;
        app.globalData.token = res.data.token;
        wx.showToast({
          title: '登录成功',
          icon: 'success',
          duration: 2000
        })
      }).catch((err) => {
        console.log(err)
      });
    } else {
      //用户按了拒绝按钮
      wx.showModal({
        title: '警告通知',
        content: '您点击了拒绝授权,将无法正常显示个人信息,点击确定重新获取授权。',
        success: function (res) {
          if (res.confirm) {
            wx.openSetting({
              success: (res) => {
                if (res.authSetting["scope.userInfo"]) {////如果用户重新同意了授权登录
                  user.loginByWeixin(e.detail).then(res => {
                    this.setData({
                      userInfo: res.data.userInfo
                    });
                    app.globalData.userInfo = res.data.userInfo;
                    app.globalData.token = res.data.token;
                  }).catch((err) => {
                    console.log(err)
                  });
                }
              }
            })
          }
        }
      });
    }
  },
  //退出登录
  exitLogin: function () {
    wx.showModal({
      title: '',
      confirmColor: '#b4282d',
      content: '退出登录？',
      success: function (res) {
        if (res.confirm) {
          wx.removeStorageSync('token');
          wx.removeStorageSync('userInfo');
          app.globalData.token = '';
          app.globalData.userInfo = {
            nickName: '点击去登录',
            userName: 'Hi,小主',
            avatarUrl: 'https://hahanongjiang.oss-cn-qingdao.aliyuncs.com/hahanongjiang/icon/miniprogram/default_head.png'
          };
          wx.switchTab({
            url: '/pages/index/index'
          });
        }
      }
    })
  },
  //联系电话客服
  linkwaiter: function () {
    wx.makePhoneCall({
      phoneNumber: '02989166339'
    })
  },
  //页面跳转
  toPage: function (e) {
    let that = this;
    util.request(api.ShopUser).then(function (res) {
      if (res.errno === 0) {
        console.log("会员中心", res.data);
        if (res.data.user_level_id != 0) {
          if (res.data.mobile == '' || res.data.mobile == null) {
            wx.showToast({
              title: '去认证',
              icon: 'none',
              duration: 2000,
              success: function () {
                setTimeout(function(){
                  wx.navigateTo({ url: "/pages/ucenter/member/auth/auth" })
                },2000)
              }
            })
          } else {
            wx.showToast({
              title: '认证中...',
              icon: 'none',
              duration: 2000
            })
          }
        } else {
          wx.navigateTo({url: e.currentTarget.dataset.url})
        }
      }
    });
  }
})